package com.zjitc.controller;

import com.zjitc.beans.MessageBean;
import com.zjitc.service.imp.ProductServiceImpl;
import com.zjitc.service.imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexCntroller extends BaseController {

    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private UserServiceImpl userService;

    private String PASS = "Tyhadmin";

    @RequestMapping("/index")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("admin");
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Object login(String password,HttpSession session) {
        boolean flag =false;
        String msg = "账号密码错误";
        if(PASS.equals(password)){
            flag = true;
            setSessionAdmin(session,"admin");
            msg = "登录成功";
            return new MessageBean(true,msg);
        }
        return new MessageBean(false,msg);
    }

    @RequestMapping("/getLoginPass")
    @ResponseBody
    public String getLoginPass() {
        return "Tyhadmin";
    }

    //
    @RequestMapping("/main")
    public ModelAndView main(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        if(getSessionAdmin(session) == null){
            modelAndView.setViewName("login");
        }else{
            Integer usercount = userService.getUserCount();
            Integer[] findcount = productService.initAdminMain();
            setSessionAdmin(session,"admin");
            modelAndView.setViewName("main");
            modelAndView.addObject("usercount",usercount);
            modelAndView.addObject("findcount",findcount);
        }

        return modelAndView;
    }
}
